package com.ruoyi.web.controller.common;

import com.alibaba.fastjson2.JSON;
import com.baidu.ueditor.ActionEnter;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.OssUtil;
import com.ruoyi.common.utils.file.FileUploadUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class UeditorController {

    private static final Logger log = LoggerFactory.getLogger(UeditorController.class);

    /** Ueditor配置文件存放路径 */
    @Value("${swagger.enabled}")
    private boolean enabled;

    //配置ueditor后端上传接口的验证
    @RequestMapping("/config")
    public String config(HttpServletRequest request, HttpServletResponse response, String action, MultipartFile[] upfile) throws IOException {
        if (action.equals("config")) {
            request.setCharacterEncoding("utf-8");
            response.setHeader("Content-Type", "text/html");
            //注意path不能含有中文路径
            String path = ClassUtils.getDefaultClassLoader().getResource("").getPath() + "config";
            //linux服务器
//            path = "/home/bridge/config";
            path = "C:\\bridge\\config";
            System.out.println("path = " + path);
            PrintWriter printWriter = response.getWriter();
            String eee = new ActionEnter(request, path).exec();
            printWriter.write(new ActionEnter(request, path).exec());
            printWriter.flush();
            printWriter.close();
        } else if (action.equals("uploadimage")) {
            Map<String, Object> result = new HashMap<String, Object>();
            for (MultipartFile multipartFile : upfile) {
//                String upload = FileUploadUtils.upload(RuoYiConfig.getUploadPath(),multipartFile);
//                System.out.println("upload = " + upload);
//                System.out.println("upload.sub= " + upload.substring(upload.lastIndexOf("/")));

                String upload = OssUtil.uploadMultipartFile(multipartFile);
                result.put("title", upload.substring(upload.lastIndexOf("/")+1));
                result.put("original", multipartFile.getOriginalFilename());
                result.put("state", "SUCCESS");
                result.put("url", upload);
                String jStr = JSON.toJSONString(result);
                return jStr;
            }
            return null;
        }else if(action.equals("uploadvideo")){
            Map<String, Object> result = new HashMap<String, Object>();
            for (MultipartFile multipartFile : upfile) {
//                String upload = FileUploadUtils.upload(RuoYiConfig.getUploadPath(),multipartFile);
//                System.out.println("upload = " + upload);
//                System.out.println("upload.sub = " + upload.substring(upload.lastIndexOf("/")));

                String upload = OssUtil.uploadMultipartFile(multipartFile);
                result.put("title", upload.substring(upload.lastIndexOf("/")+1));
                result.put("original", multipartFile.getOriginalFilename());
                result.put("state", "SUCCESS");
                result.put("url", upload);
                String jStr = JSON.toJSONString(result);
                return jStr;
            }
        }else if(action.equals("catchimage")){
            //秀米远程图片，传到阿里云服务器上面。
            List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
            // 获取表单参数名为"myArray[]"的值（注意要加上方括号）
            String[] myArray = request.getParameterValues("source[]");
            for(String source : myArray){
                String upload = OssUtil.uploadImageFromUrl(source);
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("source", source);
                map.put("url", upload);
                map.put("state", "SUCCESS");
                result.add(map);
            }
            String jStr = JSON.toJSONString(result);
            return jStr;
        }else{
            Map<String, Object> result = new HashMap<String, Object>();
            for (MultipartFile multipartFile : upfile) {
//                String upload = FileUploadUtils.upload(RuoYiConfig.getUploadPath(),multipartFile);
//                System.out.println("upload = " + upload);
//                System.out.println("upload.sub= " + upload.substring(upload.lastIndexOf("/")));

                String upload = OssUtil.uploadMultipartFile(multipartFile);
                result.put("title", upload.substring(upload.lastIndexOf("/")+1));
                result.put("original", multipartFile.getOriginalFilename());
                result.put("state", "SUCCESS");
                result.put("url", upload);
                String jStr = JSON.toJSONString(result);
                return jStr;
            }
        }
        return null;
    }
}

